<?php

namespace App\Store\Models\Goods;

use App\Common\Services\Common;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsBrand;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Basic\GoodsStyle;
use Illuminate\Database\Eloquent\Model;

/**
 * 盘点单
 */
class GoodsCheck extends Model
{
    protected $table = 'site_goods_check';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList($request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);

        $cp_id = $accountinfo['cp_id'];

        $store_ids = is_array($accountinfo['store_ids']) ? $accountinfo['store_ids'] : (empty($accountinfo['store_ids'])?[]:[$accountinfo['store_ids']]);

        $query = self::with(['storeinfo', 'brand', 'goods_category', 'goods_style']);

        if (!empty($store_ids)) {
            $query->whereIn('store_id', $store_ids);
        }

        // 盘点类型，1全部盘点，2部分盘点
        $check_type = $request['check_type'] ?? 0;
        if ($check_type > 0) {
            $query->where('check_type', $check_type);
        }

        // 1未盘点，2盘点中，3已盘点
        $status = $request['status'] ?? 0;
        if ($status > 0) {
            $query->where('status', $status);
        }

        $ids = $request->ids;
        $ids = is_array($ids) ? $ids : (is_array(json_decode($ids, true)) ? json_decode($ids, true) : []);
        if (count($ids) > 0) {
            $query->whereIn('id', $ids);
        }

        //时间筛选
        $start_time = (!is_numeric($request->start_time) && strtotime($request->start_time) > 0) ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = (!is_numeric($request->end_time) && strtotime($request->end_time) > 0) ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        $query->where('is_del', 1)->where('cp_id', $cp_id)->orderBy('create_at', 'desc');

        $isgetall = $request['isgetall'] ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function storeinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select('id', 'store_name');
    }

    public function goods_category()
    {
        return $this->hasOne(GoodsCategory::class, 'id', 'goods_category_id')->select('id', 'category_name');
    }

    public function goods_style()
    {
        return $this->hasOne(GoodsStyle::class, 'id', 'goods_style_id')->select('id', 'style_name');
    }

    public function brand()
    {
        return $this->hasOne(GoodsBrand::class, 'id', 'goods_brand_id')->select('id', 'brand_name');
    }
}
